﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using nanoCMS.Core;
using nanoCMS.StaticPages.WebUI.Models;
using nanoCMS.StaticPages.WebUI.Models.ViewModels;

namespace nanoCMS.StaticPages.WebUI.Controllers
{
    public class StaticPagesController : Controller 
    {
        private readonly WorldContext worldContext;
        private StaticPagesService staticPagesService; 

        public StaticPagesController(WorldContext worldContext)
        {
            this.worldContext = worldContext;
            staticPagesService = new StaticPagesService(worldContext);
        }

        public ActionResult Index()
        {
            return PartialView("Index", staticPagesService.CreateStaticPageIndexModel());
        }

        public ActionResult Save(StaticPageVM page)
        {
            if(ModelState.IsValid)
            {
                if(page.PageID == 0)
                {
                    staticPagesService.CreateNewPage(page);
                }
                else
                {
                    staticPagesService.EditPage(page);
                }
            }
            return PartialView("StaticPagesForm", staticPagesService.CreateStaticPageIndexModel());
        }

        public ActionResult Edit(int pageID)
        {
            return PartialView("StaticPagesForm", staticPagesService.CreateStaticPageEditModel(pageID));
        }

        public ActionResult Delete(int pageID)
        {
            if(ModelState.IsValid)
            {
                staticPagesService.Delete(pageID); 
            }
            return PartialView("StaticPagesForm", staticPagesService.CreateStaticPageIndexModel());
        }

        public ActionResult GetPageContent(int pageID)
        {
            return Content(staticPagesService.GetPageContent(pageID));
        }
    }
}
